!function () {
    var arr = [],payarr = [], shopArr, total = 0, desk = window.location.search,
        user = JSON.parse(localStorage.getItem('user')),
        peiname = $('.peiname'), peiadress = $('.peiadress'), peiarea = $('.peiarea'),
        mobile = $('.mobile'), peiQQ = $('.peiQQ');
    $('.addusermsgmodal').hide()
    if (desk) {
        renderbillFun(getParams().shopobj)
    } else {
        shopArr = JSON.parse(sessionStorage.getItem('shopArr'))
        renderAllBillFun()
    }
    function getParams() {
        var str = desk.substr(1);
        var strA = str.split("&");
        var obj = {};
        if (!str) return
        for (var i = 0; i < strA.length; i++) {
            obj[strA[i].split("=")[0]] = JSON.parse(decodeURIComponent(strA[i].split("=")[1]))
        }
        return obj
    }
    renderuserdetaily()
    function renderuserdetaily() {
        var arr = [
            '<div class="addusermsg addusermsg1 fl"><div>'+
                    '<div class="iconfont icon-plus" style="font-size:60px;"></div>'+
                    '<div>添加信息</div></div></div>'
        ]
        $.ajax({
            url: 'http://localhost/80edu/api.php/records/userdetaily',
            method: 'get',
            success: function (data) {
                var index = localStorage.getItem('detailyuser')
                $.each(data.records, function (k, v) {
                    if(index==v.id){
                        arr.unshift(
                            '<div class="addusermsg fl activeaddusermsg" data-index="'+v.id+'">'+
                                '<div class="addusermsg-top"></div>'+
                                '<div class="dealAddrHandler">'+
                                    '<div class="delAddrBtn mstl_deleteBtn"></div>'+
                                    '<div class="editAddrBtn mstl_editBtn"></div>'+
                                    '<div class="setDefaultBtn mstl_color">设为默认</div>'+
                                '</div>'+
                                '<div class="addrItem msgIconName"><i></i>'+v.name+'</div>'+
                                '<div class="addrItem addrPhone"><i></i>'+v.mobile+'</div>'+
                                '<div class="addrItem addrDetial"><i></i>'+v.adress+v.area+'</div>'+
                                '<div class="bottomSelected mstl_selectFlag"></div>'+
                            '</div>'
                        )
                    }else{
                        arr.unshift(
                            '<div class="addusermsg fl" data-index="'+v.id+'">'+
                                '<div class="addusermsg-top"></div>'+
                                '<div class="dealAddrHandler">'+
                                    '<div class="delAddrBtn mstl_deleteBtn"></div>'+
                                    '<div class="editAddrBtn mstl_editBtn"></div>'+
                                    '<div class="setDefaultBtn mstl_color">设为默认</div>'+
                                '</div>'+
                                '<div class="addrItem msgIconName"><i></i>'+v.name+'</div>'+
                                '<div class="addrItem addrPhone"><i></i>'+v.mobile+'</div>'+
                                '<div class="addrItem addrDetial"><i></i>'+v.adress+v.area+'</div>'+
                                '<div class="bottomSelected mstl_selectFlag"></div>'+
                            '</div>'
                        )
                    }
                })
                $('#userdetaily').html(arr.join(''))
                $('.mstl_color').on('click',tabuserdetaily)
                $('.addusermsg1').on('click', addusermsgshow)
            }
        })
    }
    function tabuserdetaily(){
       $(this).parents('.addusermsg').addClass('activeaddusermsg').siblings('.addusermsg').removeClass('activeaddusermsg')
       //存id进入本地
       var index = $(this).parents('.addusermsg').attr('data-index')
       localStorage.setItem('detailyuser',index)
    }

    function addusermsgshow() {
        $('.addusermsgmodal').show()
    }
    $('.mstl_bg').on('click', function () {
        $.ajax({
            url: 'http://localhost/80edu/api.php/records/userdetaily',
            method: 'post',
            data: {
                name: peiname.val(),
                adress: peiadress.val(),
                area: peiarea.val(),
                mobile: mobile.val(),
                qq: peiQQ.val(),
            },
            success: function (data) {
                renderuserdetaily()
                $('.addusermsgmodal').hide()
            }
        })
    })
    $('.cancelNewAddr,.cancelNewAdd').on('click', function () {
        $('.addusermsgmodal').hide()
    })
    $('.mstlSublimeBtn').on('click', function () {
        var obj = {}
        obj.liuyan = $('.msgContentBox input').val()
        obj.pay = $('.clcikBorder input[type="hidden"]').val()
        obj.name = $('.activeaddusermsg .msgIconName').text()
        obj.phone = $('.activeaddusermsg .addrPhone').text()
        obj.addrDetial = $('.activeaddusermsg .addrDetial').text()
        obj.peisongdir = $('#peisongdir').val()
        localStorage.setItem('userObj',JSON.stringify(obj))
            location.href = 'pay.html'
    })
    distrFun()
    function distrFun() {
        var arr = []
        $.ajax({
            url: 'http://localhost/80edu/api.php/records/peisong',
            method: 'get',
            success: function (data) {
                $.each(data.records, function (k, v) {
                    arr.push('<option>' + v.name + '</option>')
                })
                $('#peisongdir').html(arr.join(''))
            }
        })
    }
    $('#peisongdir').on('change', function () {
        $.ajax({
            url: 'http://localhost/80edu/api.php/records/peisong',
            method: 'get',
            data: { filter: 'name,eq,' + $('#peisongdir').val() },
            success: function (data) {
                $('#peisongpic').html('￥' + data.records[0].price + '.00')
            }
        })
    })

    $('.pay-way').on('click', function () {
        $(this).addClass('clcikBorder').siblings('.pay-way').removeClass('clcikBorder')
    })

    function renderAllBillFun() {
        var strobj = {}
        $.each(shopArr, function (k, v) {
            strobj['filter' + (k + 1)] = 'id,eq,' + v + ','
        })
        $.ajax({
            url: 'http://localhost/80edu/api.php/records/shopcar',
            method: 'get',
            data: strobj,
            success: function (data) {
                $.each(data.records, function (k, v) {
                    renderbillFun(v)
                })
            }
        })
    }
    function renderbillFun(data) {
        payarr.push(data)
        localStorage.setItem('payarr',JSON.stringify(payarr))
        if (data.id) {
            total += data.price * data.num
            $('.totalmoney').html('￥' + total)
            $('.shouldmoney').html(total)
        } else {
            $('.totalmoney').html('￥' + data.price * data.num)
            $('.shouldmoney').html(data.price * data.num)
        }
        arr.push(
            '<div class="inventory-concent"><div class= "inventory_uname">' +
            '<img src="./img/ABUIABACGAAgiZi55gUo1sbrigQwwB840Bc!60x60.jpg">' +
            '<p class="itemname">' + data.goodsname + '</p></div><div class="inventory_pic">' +
            '<div class="fr itemtotal">￥' + data.price * data.num + '</div>' +
            '<div class="fr itemnum">' + data.num + '</div>' +
            '<div class="fr">￥' + data.price + '</div>' +
            '</div></div>'
        )
        $('#renderBox').html(arr.join(''))
    }
}()